package com.yangjian.mian;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

//E:/test/stu.txt

class JJStuIO {
	
	static String filePath = "E:/test/stud.txt";
	
	static ArrayList <JJStudent>list;
	
	static {
		list = new ArrayList<JJStudent>();
		try {
			//���ļ���ȡ�ַ�
			//bfreader��readline����,�ɶ���
			//��ȡ�ַ�(������bfreader)
			//
			BufferedReader bfReader = 
					new BufferedReader(
					new FileReader(filePath));
			while (true) {
				String line = bfReader.readLine();
				if (line == null) {
					break;
				}

				line.trim();
				line = line.replaceAll("\\s","");
				if (line.length() == 0) {
					continue;
				}
				JJStudent s = stringToStudent(line);
				list.add(s);
			}
			
			bfReader.close();
			//��ȡ�ֽ�
//			FileInputStream in = new FileInputStream(filePath);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static JJStudent stringToStudent(String line) {
		
		String[] str = line.split(",");
		
		JJStudent s = new JJStudent(
				Integer.parseInt(str[0]),
				str[1],
				str[2],
				Integer.valueOf(str[3]),
				Integer.valueOf(str[4]));
		
		return s;
	}
	
	public static void ppp() {
		
		for (JJStudent student : list) {
			System.out.println(student);
		}
		
	}

	public static void save() {
		try {
			BufferedWriter bfWriter = new BufferedWriter(new FileWriter(filePath));
			for (JJStudent student : list) {
				String str = student.id 
						+ "," 
						+ student.name 
						+ "," 
						+ student.gender 
						+ "," + student.age
						+ "," 
						+ student.times
						+ "\n";
				
				bfWriter.write(str);
				
			}
			
			bfWriter.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		
	}
	
	
}
